//
//  SecrityLogic.m
//  MyApplication
//
//  Created by issac on 15/7/28.
//  Copyright (c) 2015年 52xiaoluo. All rights reserved.
//

#import "SecrityLogic.h"
#import "AESCrypt.h"

#import "account.pb.h"
@interface SecrityLogic()
{
    NSData *defaultKey;
    NSData *defaultIv;
}
@end
@implementation SecrityLogic

static char sDefaultKey[] = {0x0,0x1,0x2,0x3,0x4,0x5,0x6,0x7,0x8,0x9,0x20,0x21,0x22,0x23,0x24,0x25};
static char sDefaultIv[] = {0x0,0x1,0x2,0x3,0x4,0x5,0x6,0x7,0x8,0x9,0x30,0x31,0x32,0x33,0x34,0x35};
static SecrityLogic* _instance = nil;

+(instancetype) shareInstance
{
    static dispatch_once_t onceToken ;
    dispatch_once(&onceToken, ^{
        _instance = [[SecrityLogic alloc] init] ;
    });
    
    return _instance ;
}

-(id)init
{
    self = [super init];
    if (self) {
        defaultKey = [[NSData alloc ]initWithBytes:sDefaultKey length:sizeof(sDefaultKey)];
        defaultIv = [[NSData alloc ]initWithBytes:sDefaultIv length:sizeof(sDefaultIv)];
    }
    return self;
}


-(NSData *)encryptDefault:(NSData *)data {
    return [AESCrypt AESencrypt:data pwdKey:defaultKey andIv:defaultIv];
}

-(NSData *)decryptDefault:(NSData *)data {
    return [AESCrypt AESdecrypt:data pwdKey:defaultKey andIv:defaultIv];
}

-(NSData *)encryptSession:(NSData *)data {
    Session *session = [[UserLogic shareInstance] getSession];
    if (session == nil ) {
        return nil;
    }
    return [AESCrypt AESencrypt:data pwdKey:session.key andIv:session.iv];
}

-(NSData *)decryptSession:(NSData *)data {
    Session *session = [[UserLogic shareInstance] getSession];
    if (session == nil ) {
        return nil;
    }
    return [AESCrypt AESdecrypt:data pwdKey:session.key andIv:session.iv];
}

@end
